// src/controller/album/index.js
const { success, error } = require("@/utils/response");
const AlbumService = require("@/service/album");
const {uploadToQiniu} = require("@/utils/qiniuUpload");

class AlbumController {
  /**
   * @description: 相册列表
   * @param {Object} ctx - Koa 上下文对象
   * @return {Promise<void>}
   */
  async list(ctx) {
    const { pageSize, pageIndex } = ctx.query;
    const result = await AlbumService.getAlbums({ pageSize, pageIndex });
    success(ctx, result);
  }

  async detail(ctx) {
    const { id } = ctx.params;
    const album = await AlbumService.getAlbumById(id);
    success(ctx, album, "查询成功");
  }

  async save(ctx) {
    // const {files , ...data} = ctx.request.body;
    const data = ctx.request.body;
    const userId = ctx.state.user?.id;
    const album = await AlbumService.saveAlbum({userId,...data});
    success(ctx, album, "保存成功");
  }

  async delete(ctx) {
    const { id } = ctx.request.body;
    await AlbumService.deleteAlbum(id);
    success(ctx, null, "删除成功");
  }

  // 分类
  saveCategory(ctx) {
    const data = ctx.request.body;
    const category = AlbumService.saveCategory(data);
    success(ctx, category, "保存分类成功");
  }

  async listCategory(ctx) {
    const result = await AlbumService.getCategories();
    success(ctx, result, "查询分类成功");
  }

  async detailCategory(ctx) {
    const { id } = ctx.params;
    const category = await AlbumService.getCategoryById(id);
    success(ctx, category, "查询成功");
  }

  async deleteCategory(ctx) {
    const { id } = ctx.request.body;
    await AlbumService.deleteCategory(id);
    success(ctx, null, "删除成功");
  }

  async savePhoto(ctx) {
    const data = ctx.request.body;
    if (!data.albumId) {
      return error(ctx, "相册ID不能为空", 400);
    }

    const files = ctx.request.files || [];
    // 兼容单文件上传
    const fileArray = Array.isArray(files) ? files : [files];
    if (!fileArray.length || !fileArray[0]) {
      return error(ctx, "请上传照片", 400);
    }

    const photoList = [];
    for (const file of fileArray) {
      const fileInfo = file.files;
      // 上传到七牛云
      const url = await uploadToQiniu(fileInfo);
      // 组装照片参数
      const params = {
        albumId: data.albumId,
        url,
        name: fileInfo.originalFilename || fileInfo.name || '未命名',
        user_id: ctx.state.user.id || 1,
        description: data.description || "",
        order: data.order || 0,
        size: fileInfo.size || 0,
        mime_type: fileInfo.mimetype || fileInfo.type || '',
      };
      // 保存到数据库
      const photo = await AlbumService.savePhoto(params);
      photoList.push(photo);
    }

    success(ctx, photoList, "上传成功");
  }

  async deletePhoto(ctx) { 
    const { id } = ctx.request.body;
    await AlbumService.deletePhoto(id);
    success(ctx, null, "删除成功");
  }

  async getPhotoList(ctx) { 
    const params = ctx.request.body;
    try {
      const result = await AlbumService.getPhotoList(params);
      success(ctx, result, "查询成功");
    } catch (err) {
      error(ctx, error.message, 500)
    }

  }


}

module.exports = new AlbumController();